<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-select
          @visible-change="visibleChangeStreet"
          @change="changeStreet"
          v-model="query.streetId"
          placeholder="请选择街道"
          class="handle-select mr10"
        >
          <el-option
            v-for="(item, index) in streetList"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-select
          @visible-change="visibleChangeCommunity"
          @change="changeCommunity"
          v-model="query.communityId"
          placeholder="请选择社区"
          class="handle-select mr10"
        >
          <el-option
            v-for="(item, index) in communityList"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>

        <el-select
          @visible-change="visibleChangeVillage"
          @change="changeVillage"
          v-model="query.villageId"
          placeholder="请选择小区"
          class="handle-select mr10"
        >
          <el-option
            v-for="(item, index) in villageList"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>

        <el-button type="primary" @click="handleSearch">查询</el-button>

        <el-button type="primary" @click="handleAdd">添加</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          prop="index"
          label="序号"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="districtName"
          label="街道"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="communityName"
          label="社区"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="villageName"
          label="小区"
          align="center"
        ></el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="openNewPage(scope.$index, scope.row)"
              >预览</el-button
            >
            <el-button type="text" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "bigscreenmanage",
  data() {
    return {
      query: {
        address: "",
        name: "",
        pageIndex: 1,
        pageSize: 20,

        streetId: "",
        communityId: "",
        villageId: "",
      },
      tableData: [],
      streetList: [],
      communityList: [],
      villageList: [],

      multipleSelection: [],
      delList: [],
      editVisible: false,
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1,
    };
  },
  created() {
    this.getBSMData();
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      console.log(newVal, oldVal);
      if (newVal == "/big-screen-manage") {
        this.getBSMData();
      }
    },
  },
  methods: {
    // 选择街道
    changeStreet(currentStreet) {
      console.log(this.query.streetId, currentStreet);
      this.query.communityId = "";
      this.query.villageId = "";
      this.communityList = [];
      this.villageList = [];
    },
    visibleChangeStreet(bool) {
      if (bool) {
        this.getStreetList();
      }
      console.log(bool);
    },
    changeCommunity(currentCommunity) {
      console.log(this.query.communityId, currentCommunity);
      this.query.villageId = "";
      this.villageList = [];
    },
    visibleChangeCommunity(bool) {
      if (bool) {
        this.getCommunityList();
      }
      console.log(bool);
    },

    changeVillage(currentVillage) {
      console.log(this.query.villageId, currentVillage);
    },
    visibleChangeVillage(bool) {
      if (bool) {
        this.getVillageList();
      }
      console.log(bool);
    },
    getStreetList() {
      this.$axios
        .get("/village/subdistrict/page", {
          params: {
            parentId: 0,
          },
        })
        .then(({ data: res }) => {
          console.log("街道数据", res);
          if (res.code != 0) {
            return;
          }
          this.streetList = res.data.list;
        })
        .catch(() => {});
    },
    getCommunityList() {
      if (!this.query.streetId) {
        return this.$message.warning({
          message: "请先选择街道",
          duration: 1500,
        });
      }

      // communityList
      this.$axios
        .get("/village/subdistrict/page", {
          params: {
            parentId: this.query.streetId,
          },
        })
        .then(({ data: res }) => {
          console.log("社区数据", res);
          if (res.code != 0) {
            return;
          }
          this.communityList = res.data.list;
        })
        .catch(() => {});
    },
    getVillageList() {
      if (!this.query.communityId) {
        return this.$message.warning({
          message: "请先选择社区",
          duration: 1500,
        });
      }

      this.$axios
        .get("/village/subdistrict/page", {
          params: {
            parentId: this.query.communityId,
          },
        })
        .then(({ data: res }) => {
          console.log("小区数据", res);
          if (res.code != 0) {
            return;
          }
          this.villageList = res.data.list;
        })
        .catch(() => {});
    },
    // 获取 easy-mock 的模拟数据
    getBSMData() {
      this.$axios
        .get("/screen/screendata/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            districtId: this.query.streetId,
            communityId: this.query.communityId,
            villageId: this.query.villageId,
          },
        })
        .then(({ data: res }) => {
          console.log("大屏数据", res);
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.tableData.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
          });
          this.pageTotal = res.data.total;
        })
        .catch((err) => {});
    },
    handleSearch() {
      this.getBSMData();
    },
    handleAdd() {
      this.$router.push({
        path: "/big-screen-add",
      });
    },
    // 删除操作
    handleDelete(index, row) {
      this.ids = [];
      var ids = this.ids;
      ids[0] = row.id;
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delete(ids);
        })
        .catch(() => {});
    },
    delete(ids) {
      this.$axios
        .delete("/screen/screendata", {
          data: ids,
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.$message.success("操作成功");
          this.getBSMData();
        })
        .catch(() => {});
    },

    // 预览
    openNewPage(index, row) {
      // 线上
      window.open(
        `http://vue.bangning.vip/big-screen/index.html?id=${row.id}&villageId=${row.villageId}`
      );
      // 本地
      // window.open(
      //   `http://192.168.3.80:8200?id=${row.id}&villageId=${row.villageId}`
      // );
    },
    // 编辑操作
    handleEdit(index, row) {
      this.$router.push({
        path: "/big-screen-add",
        query: {
          id: row.id,
        },
      });
    },

    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getBSMData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getBSMData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 120px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
